#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2019-06-20 10:44:34

import os
import sys
import csv
import subprocess
from pathlib import Path

from argparse import ArgumentParser


def worker(*args):
    for _ in range(100):
        print(*args)
        __import__("time").sleep(0.5)


def run_in_cmd(*args):
    this_file = Path(__file__).absolute()
    this_dir = this_file.parent
    cmd = 'start cmd /K "python {} {}"'.format(this_file.name, " ".join(args))
    env = os.environ.copy()
    env["PATH"] = "{};{}".format(Path(sys.executable).parent, os.getenv("path"))
    subprocess.call(cmd, shell=True, env=env, cwd=this_dir)


def main():
    parser = ArgumentParser(description="JoinQuant finance client service",
                            epilog="Run a backend server by default")
    parser.add_argument("-f", "--csv-file", help="csv file path")
    parser.add_argument("parameters", nargs="*", help="some parameters")

    args = parser.parse_args()

    if args.parameters:
        worker(*args.parameters)
    elif args.csv_file:
        with open(args.csv_file) as fp:
            for row in csv.reader(fp):
                run_in_cmd(*row)
    else:
        parser.print_help()


if __name__ == "__main__":
    main()
